// pages/mine/mine.js
import http from "../../utils/http.js"
import QRCode from '../../utils/weapp-qrcode.js'
const app = getApp()

const W = wx.getSystemInfoSync().windowWidth;
const rate = 750.0 / W;
const qrcodeWidth = 290 / rate;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qrcodeShow: false,
    serviceMobile: '',
    nickname: "",
    avater: "",
    qrcode: null,
    qrcodeWidth: qrcodeWidth
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      serviceMobile: app.globalData.serviceMobile
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getInfo()
    
    app.globalData.lastPath = "/pages/mine/mine"
    if(app.globalData.needLogin){
      var token = wx.getStorageSync("token")
      wx.showModal({
        title: '提示',
        content: '您还未登录，请去首页登录！',
        showCancel: false,
        success (res) {
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/index/index',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: app.globalData.shareTitle,
      path: '/pages/index/index'
    }
  },
  getInfo(){
    http.request("/home/index")
    .then(res=>{
      this.setData({
        avater: res.data.avatar,
        nickname: res.data.nickname
      })
      this.createQrcode(res.data.id+'')
    })
    .catch(res=>{

    })
  },
  createQrcode(text){
    this.data.qrcode = new QRCode('canvas', {
      // usingIn: this,
      text: text,
      // image: '/images/bg.jpg',
      // padding: 12,
      width: qrcodeWidth,
      height: qrcodeWidth,
      // colorDark: "#1CA4FC",
      // colorLight: "white",
      correctLevel: QRCode.CorrectLevel.H,
      callback: (res) => {
        // 生成二维码的临时文件
        console.log(res.path)
      }
    });
  },
  naviToEditPerson(){
    wx.navigateTo({
      url: '/pages/editPerson/editPerson',
    })
  },
  naviToDrinkApply(){
    wx.navigateTo({
      url: '/pages/drinkApply/drinkApply',
    })
  },
  naviToDrinkInfo(){
    wx.navigateTo({
      url: '/pages/drinkerInfo/drinkerInfo',
    })
  },
  naviToPlan(){
    wx.navigateTo({
      url: '/pages/plan/plan',
    })
  },
  openQrcodeShow(){
    this.setData({
      qrcodeShow: true
    })
  },
  scanCode() {
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        let params = {
          friendId: res.result
        }
        http.request("/drinkUsers/scanFriend", params)
          .then(res => {
            wx.showToast({
              title: res.massage,
              icon: 'none'
            })
          })
          .catch(res => {

          })
      }
    })
  },
  onClose(){
    this.setData({
      qrcodeShow: false
    })
  },
  naviToDrinking(){
    wx.navigateTo({
      url: '/pages/drinking/drinking',
    })
  },
  telCall(){
    wx.makePhoneCall({
      phoneNumber: this.data.serviceMobile,
    })
  },
  naviToSuggest(){
    wx.navigateTo({
      url: '/pages/suggest/suggest',
    })
  },
  naviToPoster(){
    wx.navigateTo({
      url: '/pages/poster/poster',
    })
  }
})